﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetSNS.Model;
using NetSNS.Common;
using Discuz.Toolkit;

namespace Icst.Leaflike.Web
{
    public partial class register : System.Web.UI.Page
    {
        protected string error = string.Empty;
        protected string email = string.Empty;
        protected bool succeed = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Request.Form["Register"] == "Submit")
                //{
                //    DoRegister();
                //}
                //if (!succeed)
                //{

                //}
                
            }
        }

        protected void btnRegister_Click(object sender, ImageClickEventArgs e)
        {
            UserInfo ui = new UserInfo();
            ui.Email = txtEmail.Text;
            ui.Username = txtUserName.Text;
            ui.Password = txtPwd.Text;
            try
            {

                DiscuzSession ds = Discuz.ConformityHandler.InitDiscuzSession();
                ds.Register(Discuz.ConformityHandler.CreateDiscuzNTUsername(ui.Email), ui.Password, ui.Email, false);
            }
            catch (DiscuzException d)
            {
                if (d.ErrorCode == 109)
                {
                    error = "该Email地址已注册过帐户";
                }
                else
                    succeed = true;
            }
            if (succeed)
            {
                Response.Write("<script>alert('注册成功')</script>");
                Response.Redirect("index.aspx");
                
            }
            else
                Response.Write("<script>alert(error)</script>");

            
        }

        protected void DoRegister()
        {
            UserInfo ui = new UserInfo();
            ui.Email = txtEmail.Text;
            ui.Username = txtUserName.Text;
            ui.Password = txtPwd.Text;
            try
            {

                DiscuzSession ds = Discuz.ConformityHandler.InitDiscuzSession();
                ds.Register(Discuz.ConformityHandler.CreateDiscuzNTUsername(ui.Email), ui.Password, ui.Email, false);
            }
            catch (DiscuzException d)
            {
                if (d.ErrorCode == 109)
                {
                    error = "该Email地址已注册过帐户";
                }
                else
                    succeed = true;
            }
            if (succeed)
            {
                Response.Write("<script>alert('注册成功')</script>");
                Response.Redirect("index.aspx");

            }
            else
                Response.Write("<script>alert(error)</script>");
        }
    }
}
